package com.example.sunmiv1.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.RepaymentRecordAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.entity.RepaymentRecordInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RepaymentRecordActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.repaymentRecord_startTime)
    TextView repaymentRecordStartTime;
    @BindView(R.id.repaymentRecord_endTime)
    TextView repaymentRecordEndTime;
    @BindView(R.id.repaymentRecord_edit)
    EditText repaymentRecordEdit;
    @BindView(R.id.repaymentRecord_search)
    ImageView repaymentRecordSearch;
    @BindView(R.id.repaymentRecord_listView)
    PullToRefreshListView repaymentRecordListView;
    private LoadingDialog loadingDialog;
    private int page = 1;

    private String startTime = "", endTime = "";
    private RepaymentRecordInfo repaymentRecordInfo;
    private RepaymentRecordAdapter repaymentRecordAdapter;
    private List<RepaymentRecordInfo.DataBean.ListBean> list = new ArrayList<>();

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    list.addAll(repaymentRecordInfo.getData().getList());
                    setRepaymentRecordAdapter();
                    break;
            }
        }
    };

    @Override
    protected void initView() {
        setContentView(R.layout.activity_repayment_record);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.repaymentTitle);
        loadingDialog = new LoadingDialog(this);
    }

    @Override
    protected void initData() {
        getRepayRecord();
    }

    @Override
    protected void initListener() {
        repaymentRecordListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                list.clear();
                setRepaymentRecordAdapter();
                getRepayRecord();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getRepayRecord();
            }
        });
    }

    private void setRepaymentRecordAdapter() {
        if (repaymentRecordAdapter == null) {
            repaymentRecordAdapter = new RepaymentRecordAdapter(list);
            repaymentRecordListView.setAdapter(repaymentRecordAdapter);
        } else {
            repaymentRecordAdapter.notifyDataSetChanged();
        }
    }

    /**
     * -
     * 獲取還款記錄
     */
    private void getRepayRecord() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("credit_id", getIntent().getStringExtra("id"));
            jsonObject.put("page", page);
            jsonObject.put("limit", "20");
            jsonObject.put("order_no", repaymentRecordEdit.getText().toString().trim());
            jsonObject.put("start_time", startTime);
            jsonObject.put("end_time", endTime);
            jsonObject = Tools.createParameterCode(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        HttpUtils.post(context, Common.GET_REPAY_RECORD, jsonObject, new TextCallBack() {
            @Override
            protected void onSuccess(String text) {
                loadingDialog.dismiss();
                repaymentRecordListView.onRefreshComplete();
                try {
                    JSONObject json = new JSONObject(text);
                    Toast.makeText(context, json.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (json.getInt("status") == 1) {
                        if (json.getJSONObject("data").getJSONArray("list").length() > 0) {
                            repaymentRecordInfo = GsonUtils.JsonToBean(text, RepaymentRecordInfo.class);
                            Message msg = new Message();
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(ResponseException e) {
                loadingDialog.dismiss();
                repaymentRecordListView.onRefreshComplete();
            }
        });
    }


    @OnClick({R.id.title_back, R.id.repaymentRecord_startTime, R.id.repaymentRecord_endTime, R.id.repaymentRecord_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.repaymentRecord_startTime:
                repaymentRecordStartTime.setText("");
                startTime = "";
                Tools.getDateFromDatePickerDialog(repaymentRecordStartTime, time -> {
                    startTime = time + " 00:00:00";
                });
                break;
            case R.id.repaymentRecord_endTime:
                repaymentRecordEndTime.setText("");
                endTime = "";
                Tools.getDateFromDatePickerDialog(repaymentRecordEndTime, time -> {
                    endTime = time + " 23:59:59";
                });
                break;
            case R.id.repaymentRecord_search:
                page = 1;
                list.clear();
                setRepaymentRecordAdapter();
                getRepayRecord();
                break;
        }
    }
}
